package com.snow.campuslink.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
/**
 * @ClassName CodeGenerator
 * @Description TODO
 * @Author Gaosl
 * @Date 2020/12/5 20:46
 * @Version 1.0
 */
public class CodeGenerator {
    public static void main(String[] args) {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        //绝对路径
        gc.setOutputDir("C:\\mybatis-plusOut");
        gc.setAuthor("Gaosl");

        //生成后是否打开资源管理器
        gc.setOpen(true);
        //重新生成时文件是否覆盖
        gc.setFileOverride(true);
//        //去掉Service接口的首字母I
//        gc.setServiceName("%sService");
        //主键策略
        gc.setIdType(IdType.ID_WORKER_STR);
        //定义生成的实体类中日期类型
        gc.setDateType(DateType.ONLY_DATE);
        //开启Swagger2模式
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://47.119.163.105:3306/cczuCampusForum?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.qxczh.rest");
        //模块名
        pc.setModuleName("");
        pc.setController("icontroller");
        pc.setEntity("entity");
        pc.setService("iservice");
        pc.setServiceImpl("iservice.impl");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);
        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("cczu_forum_user","cczu_forum_adminuser","cczu_forum_adminuser_role"
                ,"cczu_forum_collect","cczu_forum_label","cczu_forum_notification","cczu_forum_permission"
                ,"cczu_forum_posts","cczu_forum_reply","cczu_forum_role","cczu_forum_role_permission");
        //数据库表映射到实体的命名策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //生成实体时去掉表前缀
        //strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setTablePrefix( "cczu_forum_");
        //数据库表字段映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // lombok 模型"" @Accessors(chain = true) setter链式操作
        strategy.setEntityLombokModel(true);
        //restful api风格控制器
        strategy.setRestControllerStyle(true);
        //url中驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        // 6、执行
        mpg.execute();
    }

}
